import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { StorageProvider } from '../../providers/storage/storage';
import { HttpserviesProvider } from '../../providers/httpservies/httpservies';

/**
 * Generated class for the RegisterpwdPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-registerpwd',
  templateUrl: 'registerpwd.html',
})
export class RegisterpwdPage {

  public password = '';       //密码
  public affirmPassword = ''; //确认密码
  public tel = '';            //电话号码
  public code = '';           //验证码

  constructor(public navCtrl: NavController, public navParams: NavParams,public storage:StorageProvider, public httpService:HttpserviesProvider) {

    this.tel = this.storage.get('register_tel');
    this.code = this.storage.get('code');

  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad RegisterpwdPage');
  }

  registerUserData(){

    if(!this.password.length){
      alert('请输入密码');
      return;
    }
    if(!this.affirmPassword.length){
      alert('请输入确认密码');
      return;
    }
    if(this.password.length < 6){
      alert('密码长度至少为6位');
      return;
    }
    if(this.password != this.affirmPassword){
      alert('俩次输入的密码不一致');
      return;
    }

    //api/register
    var api = 'api/register';
    this.httpService.doPostData(api,
      {'tel':this.tel,'code':this.code,'password':this.password},
      (data)=>{
      console.log(data);
      if(data.success){
        //注册成功
        this.storage.set('userinfo',data.userinfo[0]);
        this.navCtrl.popToRoot();   /*回到根页面*/
      }else{
        alert('注册失败');
      }

    })

  }

}
